Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add consistent logging errorprone rule #1644

Merged
merged 8 commits into from
Feb 12, 2021

Conversation

ferozco
Copy link
Contributor

@ferozco ferozco commented Feb 11, 2021

Before this PR

Closes #1642

After this PR

==COMMIT_MSG==
Add consistent logging errorprone rule
==COMMIT_MSG==

Possible downsides?

@policy-bot policy-bot bot requested a review from jkozlowski February 11, 2021 15:57
@iamdanfox
Copy link
Contributor

Depending on how controversial this is, we might consider auto-fixing this by adding here

private static final ImmutableList<String> DEFAULT_PATCH_CHECKS = ImmutableList.of(

summary = "Loggers created using getLogger(Class<?>) must be named 'log'.")
public final class ConsistentLoggerName extends BugChecker implements BugChecker.VariableTreeMatcher {

private static final Matcher<VariableTree> matcher = Matchers.allOf(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on filtering down to static final fields in this case? I think we should add other rules to require loggers to be private+static+final, but in the case of odd legacy code we should flag the most specific failure. I'm not confident we'd want instance loggers to be renamed automatically, I just don't have a good enough sense for all the places they might exist.

@carterkozak
Copy link
Contributor

+1 for auto-fixing this, please give the automatic fix a run on our large internal codebase to verify before we merge.

if (!matcher.matches(tree, state)) {
return Description.NO_MATCH;
if (matcher.matches(tree, state)) {
if (!tree.getName().contentEquals("log")) {
Copy link
Contributor

@carterkozak carterkozak Feb 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pedantic nit: might as well combine conditions to reduce indent depth if (matcher.matches(tree, state) && !tree.getName().contentEquals("log")) {

I'll give this PR a try internally

private static final Matcher<VariableTree> matcher = Matchers.allOf(
Matchers.isField(),
Matchers.isStatic(),
MoreMatchers.isFinal(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can avoid adding a new utility:

Suggested change
MoreMatchers.isFinal(),
Matchers.hasModifier(Modifier.FINAL),

That will match interface Iface { Logger log = LoggerFactory.getLogger(Iface.class); } as final which is why we had to implement the custom MoreMatchers.hasExplicitModifier for our RedundantModifier check.

"Test.java",
"import org.slf4j.*;",
"class Test {",
" private static final Logger LOG = LoggerFactory.getLogger(Test.class);",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about logger -> log? or do you think that's cool?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything goes to log. There can only be 1 name for loggers

Copy link
Contributor

@carterkozak carterkozak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@bulldozer-bot bulldozer-bot bot merged commit 43630b8 into develop Feb 12, 2021
@bulldozer-bot bulldozer-bot bot deleted the fo/consistent-logging branch February 12, 2021 19:27
@svc-autorelease
Copy link
Collaborator

Released 3.68.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enforce casing and naming of slf4j static Logger field
5 participants